package com.qf.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.blog.auth.LoginThreadLocal;
import com.qf.blog.constatns.LoginConstatns;
import com.qf.blog.entity.User;
import com.qf.blog.service.IUserService;
import com.qf.blog.service.impl.FollowerService;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author toString
 * @since 2024-05-22
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private FollowerService followerService;

    @Autowired
    private IUserService userService;
    @RequestMapping("/password")
    public String ChangePassword(String oldPassword, String newPassword, HttpServletRequest request, Model model){

        User loginUser = (User) request.getSession().getAttribute(LoginConstatns.LOGIN_USER_KEY);
        User user = userService.getById(loginUser);

        String confirmPassword = request.getParameter("confirmPassword");
        if (user == null) {
            throw new RuntimeException("请先登录");
        }

        if(StringUtils.isEmpty(oldPassword)){
            throw new RuntimeException("原密码输入不能为空");
        }
        if(StringUtils.isEmpty(newPassword)){
            throw new RuntimeException("新密码不能为空");
        }
        //if(user.getPassword() == null){
        //    throw new RuntimeException("原密码不存在");
        //}

        if (!BCrypt.checkpw(oldPassword,user.getPassword())) {
            throw new RuntimeException("原密码输入错误");
        }
        if(oldPassword.equals(newPassword)){
            throw new RuntimeException("新密码与原密码一致");
        }
        if(!confirmPassword.equals(newPassword)){
            throw new RuntimeException("确认密码输入错误");
        }
        String hashpw = BCrypt.hashpw(newPassword, BCrypt.gensalt());
        user.setPassword(hashpw);
        boolean b = userService.updateById(user);
        if(!b){
            throw new RuntimeException("修改失败");
        }
        return "/site/login";
    }

    @RequestMapping("/info/{userId}")
    public String account(@PathVariable Integer userId,Model model){
        User user = userService.getById(userId);
        System.out.println("user = " + user);
        user.setPassword(null);
        Integer loginId = LoginThreadLocal.get().getId();
        Boolean isFollower = followerService.IsFollower(loginId,userId.toString());

        Long followerCount = followerService.getFollowerCount(userId);
        Long fansCount = followerService.getFansCount(userId);
        Long starsCount = followerService.getStarsCount(userId);

        model.addAttribute("user", user);
        model.addAttribute("isFollower", isFollower);
        model.addAttribute("followerCount", followerCount);
        model.addAttribute("fansCount", fansCount);
        model.addAttribute("starsCount", starsCount);
        return "site/profile";
    }
}
